<?php 
	namespace Home\Controller;
	use Home\Controller\CommonController;
	use Home\Model\Users;
	class UserController extends CommonController{
		public $user;
		public function __init(){
			$this->user=new Users;
		}

		public function index(){
			$count=$this->user->whereNotIn('class',array(2))->count();
			$page=Page::desc(['pre'=>'上一页', 'next'=>'下一页'])->row(10)->make($count);
			$data=$this->user->whereNotIn('class',array(2))->limit(Page::limit())->get();
			foreach($data as $k=>$v){
				switch($v['class']){
					case 0:
						$data[$k]['class']='客服';break;
					case 1:
					$data[$k]['class']='销售';break;
					case 2:
					$data[$k]['class']='管理员';break;
				}
			}
			View::with('page',$page);
			View::with('data',$data);
			View::make('index');
		}

		public function addUser(){
			if(IS_POST){
				$res=$this->user->addData();
				if($res){
					View::success('添加成功',U('index'));
				}else{
					View::error($this->getError());
				}
			}
			View::make();
		}

		public function editUser(){
			$uid=q('get.uid');
			$data=$this->user->where('uid',$uid)->find();
			if(IS_POST){
				// p($_POST);
				$res=$this->user->editUser();
				if($res){
					View::success('修改成功',U('index'));
				}else{
					View::error('修改失败',U('index'));
				}
			}
			View::with('data',$data);
			View::make();
		}

		//锁定用户
		public function lockUser(){
			// p(q('post.uid'));
			$this->user->where('uid',q('post.uid'))->update(array('statu'=>1));
			View::ajax(array('statu'=>1));
		}
		//解锁用户
		public function unLockUser(){
			// p(q('post.uid'));
			$this->user->where('uid',q('post.uid'))->update(array('statu'=>0));
			View::ajax(array('statu'=>1));
		}
	}
?>